<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<%@ page import="com.starbaba.core.web.constant.WebConstants"%>
<%@ page import="com.starbaba.system.user.model.UserVO"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-Hans">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<head>
		<%@ include file="/common/common-css.v2.jsp" %>
		<link href="${ctx}/css/bootstrap-multiselect.css" rel="stylesheet" />
		
		<style>
		#jquery-table td{word-break: break-all;word-wrap: break-word;}
		.load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
		.load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
		a.check{
			margin-bottom:10px;
		}
		b.caret{
			margin-top: 0 !important;
		}
		.datepicker.dropdown-menu{
			z-index: 99998 !important;
		}
		.discovery-alert {
		    position: fixed;
		    width: 300px;
		    z-index: 99999;
		    left: 50%;
		    margin-left: -150px;
		    top: 300px;
		    display: none;
		}
		@media (min-width: 768px) and  (max-width: 1200px){
			.backup-text{
				width:100px !important;
			}
		}
		</style>
	</head>
<body  class="no-skin">
<div  class="load_bg" style="display:none;" ></div>
<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>
<div class="page-content">
<div id="test"></div>
			<div class="page-header" style="margin-bottom:0px;padding-bottom:10px;">
			
				<h1 id="fu_name">
					
				</h1>
			</div>
	<form id="queryForm" >
	<div class="page-content-area">
		<div class="row">
			<div class="col-sm-12">
				<div class="widget-box" style="border:0px solid #ccc; border-top:none;border-bottom:none;">
					<div class="widget-header widget-header-blue widget-header-flat">
						<h4 class="widget-title">选择条件进行查询</h4>

						<div class="widget-toolbar">
							<a href="#" data-action="collapse">
								<i class="ace-icon fa fa-chevron-up"></i>
							</a>
						</div>
					</div>

					<div class=" widget-body">
						<!-- <div class="widget-main" style="padding:12px;border:0px solid #ccc; border-top:none;border-bottom:none;"> -->
									<div class="widget-main clearfix" >

										<div class="form-group col-xs-12 col-sm-3 col-md-3 col-lg-3">
											<label class="col-sm-3 control-label" for="menuname" style="text-align:right;">菜单名:</label>
											<div class="col-sm-9">
												<input type="text" class="form-control" name="menuname" id="menuname">
											</div>
										</div>
										<div class="form-group col-xs-12 col-sm-3 col-md-3 col-lg-3">
											<label class="col-sm-3 control-label" for="menustatus" style="text-align:right;">状态:</label>
											<div class="col-sm-9">
												<select class="form-control" name="menustatus" id="menustatus"  >
													<option value="1">上线</option>
													<option value="0">下线</option>
												</select>
											</div>
										</div>
										<div class="form-group col-xs-12 col-sm-3 col-md-3 col-lg-3">
											<label class="col-sm-3 control-label" for="menutype" style="text-align:right;">层级:</label>
											<div class="col-sm-9">
												<select class="form-control menuNum" name="menutype" id="menutype">
													<option value="">-请选择菜单级别-</option>													
												</select>
											</div>
										</div>
										
							</div><!-- /.widget-main -->
					</div><!-- /.widget-body -->
				</div><!-- /.widget-box -->
			</div><!-- /.col-sm-12 -->
				<div class="col-md-12 center">
				<div class="clearfix form-actions">
				
					<button class="btn btn-purple  no-border" type="button" name="btn-query" id="btn-query">
						<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
						查询
					</button>
					&nbsp;&nbsp;&nbsp;
					<button class="btn btn-grey  no-border" type="reset">
						<i class="ace-icon fa fa-undo icon-on-right bigger-110"></i>
						重置
					</button>
					&nbsp;&nbsp;&nbsp;
					<a class="btn btn-grey  no-border" data-toggle="modal" data-target="#myModal">						
						添加
					</a>
				  </div>
		   </div>
		</div><!-- /.row -->
		
		<div class="row">
			<div class="col-xs-12">
				<table id="jquery-table" class="table table-striped table-bordered table-hover">
				
				</table>
			</div><!-- /.col -->
		</div><!-- /.row -->
			
	</div><!-- /.page-content-area -->
	</form>
</div><!-- /.page-content -->

<!--模态框-->
<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">新增菜单</h4>
      </div>
      <div class="modal-body clearfix">
	    <div class="row">
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="addmenuname">菜单名:</label>
				<div class="input-group col-sm-8">
					<input class="form-control" name="addmenuname" id="addmenuname">
				</div>
			</div>
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="addurl">地址:</label>
				<div class="input-group col-sm-8">
					<input type="text" class="form-control" name="addurl" id="addurl" placeholder="请填写相对地址">
				</div>
			</div>
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="addmenutype">层级:</label>
				<div class="input-group col-sm-8">
					<select class="form-control menuNum" name="addmenutype" id="addmenutype">	</select>
				</div>
			</div>
			<div class="form-group col-xs-10 addmenutypeforbox addmenutypeforbox1 hidden">
				<label class="col-sm-4 control-label"  for="addmenutypefor1">请选择一级父菜单:</label>
				<div class="input-group col-sm-8">
					<select name="addmenutypefor1" class="form-control menutypefor1 addmenutypefor1">
					</select>
				</div>
			</div>
			<div class="form-group col-xs-10 addmenutypeforbox addmenutypeforbox2 hidden">
				<label class="col-sm-4 control-label"  for="addmenutypefor2">请选择二级父菜单:</label>
				<div class="input-group col-sm-8">
					<select name="addmenutypefor2" class="form-control menutypefor2 addmenutypefor2">
					</select>
				</div>
			</div>
			
		</div>		
       	   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="addMenu">确认</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!--模态框-->
<div class="modal fade" id="myModal-change">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">修改菜单</h4>
      </div>
      <div class="modal-body clearfix">
	    <div class="row">
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="changemenuname">菜单名:</label>
				<div class="input-group col-sm-8">
					<input class="form-control changemenuname" name="changemenuname" id="changemenuname">
				</div>
			</div>
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="addurl">地址:</label>
				<div class="input-group col-sm-8">
					<input type="text" class="form-control changeurl" name="changeurl" id="changeurl" placeholder="请填写相对地址">
				</div>
			</div>
			<!--test 测试是不是有源数据出来-->
			<div class="form-group col-xs-10">
				<label class="col-sm-4 control-label"  for="changemenutype">层级:</label>
				<div class="input-group col-sm-8">
					<select class="form-control menuNum" name="changemenutype" id="changemenutype">
					</select>
				</div>
			</div>
			<div class="form-group col-xs-10 changemenutypeforbox changemenutypeforbox1 hidden">
				<label class="col-sm-4 control-label"  for="">请选择一级父菜单:</label>
				<div class="input-group col-sm-8">
					<select name="changemenutypefor1" class="form-control menutypefor1 changemenutypefor1">
					</select>
				</div>
			</div>
			<div class="form-group col-xs-10 changemenutypeforbox changemenutypeforbox2 hidden">
				<label class="col-sm-4 control-label"  for="">请选择二级父菜单:</label>
				<div class="input-group col-sm-8">
					<select name="changemenutypefor2" class="form-control menutypefor2 changemenutypefor2">
					</select>
				</div>
			</div>
			
			
		</div>		
       	   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="change-menu">确认</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!--警告-->
<div class="alert alert-danger alert-dismissible discovery-alert" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <span id="alert-text"></span>
</div>

<!--获取userid-->
<div id="getCreator" class="hidden"><cx:Code2Name definition="!com.starbaba.system.user.model.UserVO*username*userid" value="<%= ((UserVO) session.getAttribute(WebConstants.SESSION_ATTRIBUTE_USER)).getUserid() %>"/></div>

<%@ include file="/common/common-js.jsp" %>
<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script type="text/javascript" src="${ctx}/js/bootstrap-multiselect.js"></script>
<!-- inline scripts related to this page -->
<script type="text/javascript">
	
//定义参数
var creator = $('#getCreator').text();

var firstTime = true;

var $allData = null;

//接受URL参数，进行文档写入
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return decodeURI(r[2]); return null;
}

function toast(text, selector){
	$('#alert-text').text(text);
	$('.discovery-alert').show();
	if(selector != '' && selector != null){
		$(selector).focus();
	};
	setTimeout(function(){
		$('.discovery-alert').hide();
	},3000);
};

//【查询】按钮Click事件

function query_any(){
	if(jqTable != null){
		jqTable.fnDraw();
	}else{
		loaddata();	
	}
}
          
var jqTable = null;
var loaddata = function(){
	jQuery(function($) {
		jqTable = $('#jquery-table').dataTable( {
		    //数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
		    "columnDefs": [
		    	{	  
		    		  "title":'菜单名',
		    	  	  "targets": [0],
					  "data": "lname",   //JSON返回属性
		    	  	  "class": "center",
		    	  	  "bSortable": false,
					  "width": "25%"
		    	},
				{	  
		    		  "title":'地址',
		    	  	  "targets": [1],
					  "data": "url",   //JSON返回属性
		    	  	  "class": "center",
		    	  	  "bSortable": false,
					  "width": "25%"
		    	},
				{	  
		    		  "title":'层级',
		    	  	  "targets": [2],
					  "data": "menutype",   //JSON返回属性
		    	  	  "class": "center",
		    	  	  "bSortable": false,
					  "width": "25%",
					  "render": function(value, type, row) { // 返回自定义内容
					  		var num=new Array("","一","二","三","四","五","六");
					  		if(value<2){
					  			value=1;
					  		}
							return num[value]+"级菜单";		
					  }	
		    	},
 				{	  
		    		  "title":'创建人',
		    	  	  "targets": [3],
					  "data": "creator",   //JSON返回属性
		    	  	  "class": "center",
		    	  	  "bSortable": false,
					  "width": "25%"
		    	},
				{	  
		    		  "title":'操作',
		    	  	  "targets": [4],
					  "data": "controlid",   //JSON返回属性
		    	  	  "class": "center",
		    	  	  "bSortable": false,
					  "width": "25%",
					  "render": function(value, type, row) { // 返回自定义内容
						   return "<a class='btn btn-primary btn-change' data-controlid='"
						   +value+"' data-pcontrolid='"+row.pcontrolid+"' data-lname='"
						   +row.lname + "' data-url='" + row.url + "' >修改</a>";									 
					  },
		    	},
		  	],
			"sAjaxSource": ctx+"/system/menu/menuList.action",//数据源URL
			"fnServerData": function ( sSource, aoData, fnCallback ) {//从服务器请求数据
		   			var data = $('#queryForm').serializeObject();
					data.aoData = JSON.stringify(aoData);
					$allData = data;
					$.ajax({
						"type": "POST",
						"dataType": 'json',
						"url": sSource,
						"data": data,
						"beforeSend": function(){
							$(".load_bg,.load_img").show();
						},
						"success": function(resp){
                            console.log(resp);							

							fnCallback(resp);
							initListPage();
							if(firstTime){
								initRequest();
							}
						},
						"error": function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
						},
						"complete": function(){
							$(".load_bg,.load_img").hide();
						}
					});
					
			},
			"aLengthMenu": [10000], 
			"sScrollXInner": "100%",
			"sScrollY": "100%",//如果不设置，IE会有问题
		    "aaSorting": [[0,"asc"]], //初始化加载，默认按第几列排序、排序方式（列从0开始计算）
		});
	});
};
	
jQuery(function($) {
	
	var num='1';
	
	//异步获取菜单选项最低数
	$.ajax({
			type:"post",
			dataType: "json",
			async : false,
			url:ctx+ "/system/menu/getNum.action",
			success: function(data){
				var num=new Array("","一","二","三","四","五","六");
				for (var i = 1; i <= data.result.msg; i++) {
					$('.menuNum').append("<option value="+i+">"+num[i]+"级菜单</option>");
				}
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
				alert(textStatus);
			}
	});
	
	
	
	$(".date-picker").datepicker({autoclose:true}).next().on(ace.click_event, function(){
		$(this).prev().focus();
	});
	
	//初始化请求一次
	query_any();
	
	$('#btn-query').on('click', function(){
		query_any();
	});

	//修改权限点击事件
	$(document).delegate('.btn-change', 'click', function(){
		var controlid = $(this).attr('data-controlid');
		var lname = $(this).attr('data-lname');
		var url = $(this).attr('data-url');
		$('.changemenuname').val(lname);
		$('.changeurl').val(url=='null'?'':url);
		$('#change-menu').attr('data-controlid', controlid);
		//展示模态框
		var top = $(this).position().top;
		$('#myModal-change').css('top',top - 300).modal('show');
		
	});
	
	
	
	//新增时的父controlid
	var addpcontrolid = '00';
	
	$('#addmenutype').on('change', function(){
		var menutype = $(this).val();
		if(menutype == 1){
			$('.addmenutypeforbox').addClass('hidden');
		}else if(menutype == 2){
			$('.addmenutypeforbox2').addClass('hidden');
			$('.addmenutypeforbox1').removeClass('hidden');
		}else if(menutype == 3){
			$('.addmenutypeforbox1').addClass('hidden');
			$('.addmenutypeforbox2').removeClass('hidden');
		};
	});
	
	//修改页面 选中时联动出现
	$('#changemenutype').on('change', function(){
		var menutype = $(this).val();
		if(menutype == 1){
			$('.changemenutypeforbox').addClass('hidden');
		}else if(menutype == 2){
			$('.changemenutypeforbox2').addClass('hidden');
			$('.changemenutypeforbox1').removeClass('hidden');
		}else if(menutype == 3){
			$('.changemenutypeforbox1').addClass('hidden');
			$('.changemenutypeforbox2').removeClass('hidden');
		};
	});
	
	
	
	
	
	//添加用户确认按钮
	$('#addMenu').on('click', function(){
		var addmenuname = $('#addmenuname').val();
		var addurl = $('#addurl').val();
		var addmenutype = $('#addmenutype').val();
		
		if(addmenutype == 1){
			addpcontrolid = '00';
		}else if(addmenutype == 2){
			addpcontrolid = $('.addmenutypefor1').val();
		}else if(addmenutype == 3){
			addpcontrolid = $('.addmenutypefor2').val();
		};
		
		if(addmenuname == ''){
			toast('请输入菜单名', '#addmenuname');
		}else{
			//请求新增用户
			$.ajax({
				"type": "POST",
				"dataType": 'json',
				"url": ctx+"/system/menu/addMenu.action",
				"data": {"addmenuname":addmenuname, "addurl":addurl, 
					"creator": creator, "addmenutype": addmenutype, 
					"addpcontrolid":addpcontrolid},
				"beforeSend": function(){
					$(".load_bg,.load_img").show();
				},
				"success": function(resp){
					console.log(JSON.stringify(resp));
					initRequest();
					//隐藏模态框
					$('#myModal').modal('hide');
				},
				"error": function(XMLHttpRequest, textStatus, errorThrown){
					toast(textStatus,'');
				},
				"complete": function(){
					$(".load_bg,.load_img").hide();
				}
			});	
		}
		
		
	});
	
	
	//点击保存修改事件
	$('#change-menu').on('click', function(){
		var controlid = $(this).attr('data-controlid');
		var lname = $('#changemenuname').val();
		var eurl = $('#changeurl').val();
		
		//父级选项id获取
		var changemenutype = $('#changemenutype').val();
		
		if(changemenutype == 1){
			addpcontrolid = '00';
		}else if(changemenutype == 2){
			addpcontrolid = $('.changemenutypefor1').val();
		}else if(changemenutype == 3){
			addpcontrolid = $('.changemenutypefor2').val();
		};
		if(lname == ''||lname==null){
			toast('请输入菜单名', '#changemenuname');
			return;
		}else{
			$.ajax({
				"type": "POST",
				"dataType": 'json',
				"url": ctx+"/system/menu/changeMenu.action",
				"data": {"id":controlid, "pid":addpcontrolid,
					"changeurl":eurl, "creator": creator, 
					"menutype":changemenutype,"changemenuname": lname},
				"beforeSend": function(){
					$(".load_bg,.load_img").show();
				},
				"success": function(resp){
					console.log(JSON.stringify(resp));
					initRequest();
					//隐藏模态框
					$('#myModal-change').modal('hide');
				},
				"error": function(XMLHttpRequest, textStatus, errorThrown){
					toast(textStatus,'');
				},
				"complete": function(){
					$(".load_bg,.load_img").hide();
				}
			});	
		}
	});
	
	
});	

//把dataTable的ajax请求抽出来
function initRequest(){
	
	$allData.menustatus = 1;
	$allData.menutype = '';
	$allData.menuname = '';
	
	$.ajax({
		"type": "POST",
		"dataType": 'json',
		"url": ctx+"/system/menu/menuList.action",
		"data": $allData,
		"beforeSend": function(){
			$(".load_bg,.load_img").show();
		},
		"success": function(resp){
            console.log(resp);							
			initMenuData(resp.aaData);
			//第二次就不再在dataTable完成后再请求
			firstTime = false;
		},
		"error": function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
		},
		"complete": function(){
			$(".load_bg,.load_img").hide();
		}
	});
}

function initMenuData(list){
	//清空值
	$('select.menutypefor1,select.menutypefor2').html('');
	$.each(list, function(index, item){
		if(item.menutype == null || item.menutype == ''|| item.menutype == 1){
			//一级菜单
			var html = '<option value="' + item.controlid + '">' + item.lname + '</option>';
			$('select.menutypefor1').append(html);
		}else if(item.menutype == 2){
			//二级菜单
			$('select.menutypefor2').append('<option value="' + item.controlid + '">' + item.lname + '</option>');
		} ;
	});
};

</script>
		
</body>
</html>